package org.example.im.common.serialize;

import cn.hutool.extra.spring.SpringUtil;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * @author dingrui
 * @since 2022/2/10
 * @description
 */
@Component
public class SerializerFactory {

    public Serializer create(SerializerType type) {
        if (Objects.isNull(type)) return null;
        Map<String, Serializer> beansMap = SpringUtil.getBeansOfType(Serializer.class);
        if (Objects.isNull(beansMap)) return null;
        return beansMap.values().stream().filter(o -> Objects.equals(o.type(), type)).findFirst().orElseGet(() -> null);
    }
}
